package com.haoshuang.zhouzhoubang.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import com.haoshuang.zhouzhoubang.databinding.ActivityReturnDetailBinding
import com.haoshuang.zhouzhoubang.ui.vm.ReturnDetailViewModel
import com.mufeng.libs.base.BaseActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visibleOrGone
import java.text.DecimalFormat


/**
 *
 */
class ReturnDetailActivity : BaseActivity<ReturnDetailViewModel, ActivityReturnDetailBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "退款详情"
    }

    override fun initData() {
        var ids = intent.getIntExtra("ids", 0)
        vm.getData("order_id" to ids)
        binding.apply {
            tvNext.clickWithTrigger {
                startPage<HandleReturnActivity>("order_id" to ids)
                finish()
            }
        }

    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    binding.tvType.text = it.type
                    binding.llRefundDescription.visibleOrGone(it.remark.isNotEmpty())
                    binding.tvRefundDescription.text = it.remark
                    binding.applyMoney.text = it.apply_money
                    binding.createTime.text = it.create_time
                    if (it.define_second > 0) {
                        var num = it.define_second * 1000
                        val countDownTimer: CountDownTimer =
                            object : CountDownTimer(num.toLong(), 1 * 1000) {
                                @SuppressLint("SimpleDateFormat")
                                override fun onTick(millisUntilFinished: Long) {
                                    val s: String = formatDateTime(millisUntilFinished)
                                    //                Log.e("wy", "sysTimeStr s: "+s );
                                    binding.defineSecond.setText("剩余时间：${s}")
                                }

                                override fun onFinish() {
                                    binding.defineSecond.setText("剩余时间：00:00:00")
                                    binding.defineSecond.gone()
                                }
                            }
                        countDownTimer.start()
                    }

                }

            }
        }
    }

    fun formatDateTime(milliseconds: Long): String {
        val sb = StringBuilder()
        val mss = milliseconds / 1000
        val days = mss / (60 * 60 * 24)
        val hours = mss % (60 * 60 * 24) / (60 * 60)
        val minutes = mss % (60 * 60) / 60
        val seconds = mss % 60
        val format = DecimalFormat("00")

        if (days > 0) {
            sb.append(format.format(days)).append("天").append(format.format(hours)).append("时")
                .append(format.format(minutes)).append("分")
                .append(format.format(seconds)).append("秒")
        } else if (hours > 0) {
            sb.append("0").append("天").append(format.format(hours)).append("时")
                .append(format.format(minutes)).append("分")
                .append(format.format(seconds)).append("秒")
        } else {
            sb.append("0").append("时").append(format.format(minutes)).append("分")
                .append(format.format(seconds)).append("秒")
        }
        return sb.toString()
    }
}